package org.jet.emall.prod.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.jet.emall.common.restful.result.CommonResult;
import org.jet.emall.log.AdminOperationLog;
import org.jet.emall.log.LogType;
import org.jet.emall.prod.dto.CategoryDTO;
import org.jet.emall.prod.dto.ProdAttrDTO;
import org.jet.emall.prod.dto.ProdSpecDTO;
import org.jet.emall.prod.model.NewCategoryModel;
import org.jet.emall.prod.service.CustomProductCategoryService;
import org.jet.emall.prod.transfer.CategoryVoTransfer;
import org.jet.emall.prod.vo.CategoryVO;
import org.jet.emall.prod.vo.ProdAttrVO;
import org.jet.emall.prod.vo.ProdSpecVO;
import org.jet.emall.prod.wrapper.*;
import org.jet.emall.rbac.annotation.ApiInformation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-15 09:56
 */
@Api(value = "商品分类Controller",tags = "商品分类操作接口")
@RestController
public class CategoryController {

    @Autowired
    private CustomProductCategoryService prodCateService;

    @ApiInformation(function = "获取分类",menu = "商品分类")
    @ApiOperation(value = "获取分类")
    @RequestMapping(value = "/categories",method = RequestMethod.GET)
    public CommonResult<List<CategoryVO>> getProductCategories(){
        List<CategoryDTO> dtoList = prodCateService.getProductCategories();
        List<CategoryVO> voList = CategoryVoTransfer.INSTANCE.toVoList(dtoList);
        return CommonResult.success(voList,"获取商品分类成功");
    }



    @ApiInformation(function = "获取可显示的分类")
    @ApiOperation(value = "获取可显示的分类")
    @RequestMapping(value = "/categories/show",method = RequestMethod.GET)
    public CommonResult<List<CategoryVO>> getProdCateWithShow(){
        List<CategoryDTO> dtoList = prodCateService.getProductCategoriesOnlyShow();
        List<CategoryVO> voList = CategoryVoTransfer.INSTANCE.toVoList(dtoList);
        return CommonResult.success(voList,"获取商品分类成功");
    }


    @ApiInformation(function = "获取分类的属性")
    @ApiOperation(value = "获取分类的属性")
    @RequestMapping(value = "/categories/attrs/{cate3Id}",method = RequestMethod.GET)
    public CommonResult<List<ProdAttrVO>> getAttrsOfCate3(@PathVariable Integer cate3Id){
        List<ProdAttrDTO> attrDTOList = prodCateService.findAttrsOfCate3(cate3Id);
        List<ProdAttrVO> attrVOList =CategoryVoTransfer.INSTANCE.toAttrVoList(attrDTOList);

        return CommonResult.success(attrVOList,"获取指定分类的属性列表成功");
    }

    @ApiInformation(function = "获取分类的规格")
    @ApiOperation(value = "获取分类的规格")
    @RequestMapping(value = "/categories/specs/{cate3Id}",method = RequestMethod.GET)
    public CommonResult<List<ProdSpecVO>> getSpecsOfCate3(@PathVariable Integer cate3Id){
        List<ProdSpecDTO> specDTOList = prodCateService.findSpecsOfCate3(cate3Id);
        List<ProdSpecVO> specVOList = CategoryVoTransfer.INSTANCE.toSpecVoList(specDTOList);
        return CommonResult.success(specVOList,"获取指定分类的规格列表成功");
    }

    @ApiInformation(function = "删除规格",menu = "商品分类")
    @AdminOperationLog(value = "删除规格",logType = LogType.DELETE)
    @ApiOperation(value = "删除规格")
    @RequestMapping(value = "/categories/specs/{specId}",method = RequestMethod.DELETE)
    public CommonResult<Object> deleteSpec(@PathVariable Integer specId){
        prodCateService.deleteSpec(specId);
        return CommonResult.success(null,"删除规格成功");
    }

    @ApiInformation(function = "删除属性",menu = "商品分类")
    @AdminOperationLog(value = "删除属性",logType = LogType.DELETE)
    @ApiOperation(value = "删除属性")
    @RequestMapping(value = "/categories/attrs/{attrId}",method = RequestMethod.DELETE)
    public CommonResult<Object> deleteAttr(@PathVariable Integer attrId){
        prodCateService.deleteAttr(attrId);
        return CommonResult.success(null,"删除属性成功");
    }

    @ApiInformation(function = "添加属性",menu = "商品分类")
    @AdminOperationLog(value = "给分类添加属性",logType = LogType.ADD)
    @ApiOperation(value = "添加属性")
    @RequestMapping(value = "/categories/attrs",method = RequestMethod.POST)
    public CommonResult<Object> addAttrForCate3(@RequestBody @Valid CateIdAndAttrNameWrapper wrapper){
        prodCateService.addAttrForCate3(wrapper.getCateId(),wrapper.getAttrName());
        return CommonResult.success(null,"添加属性成功");
    }

    @ApiInformation(function = "添加规格",menu = "商品分类")
    @AdminOperationLog(value = "添加规格",logType = LogType.ADD)
    @ApiOperation(value = "添加规格")
    @RequestMapping(value = "/categories/specs",method = RequestMethod.POST)
    public CommonResult<Object> addSpecForCate3(@RequestBody @Valid CateIdAndSpecNameWrapper wrapper){

        prodCateService.addSpecForCate3(wrapper.getCateId(),wrapper.getSpecName());
        return CommonResult.success(null,"添加属性成功");
    }

    @ApiInformation(function = "修改属性名称",menu = "商品分类")
    @AdminOperationLog(value = "修改属性名称",logType = LogType.UPDATE)
    @ApiOperation(value = "修改属性")
    @RequestMapping(value = "/categories/attrs",method = RequestMethod.PATCH)
    public CommonResult<Object> updateAttr(@RequestBody @Valid ProdAttrVO prodAttrVO){
        prodCateService.updateAttr(prodAttrVO.getId(),prodAttrVO.getName());
        return CommonResult.success(null,"修改属性名成功");
    }

    @ApiInformation(function = "修改规格名称",menu = "商品分类")
    @AdminOperationLog(value = "修改规格名称",logType = LogType.UPDATE)
    @ApiOperation(value = "修改规格")
    @RequestMapping(value = "/categories/specs",method = RequestMethod.PATCH)
    public CommonResult<Object> updateSpec(@RequestBody @Valid ProdSpecVO prodSpecVO){
        prodCateService.updateSpec(prodSpecVO.getId(),prodSpecVO.getName());
        return CommonResult.success(null,"修改规格名成功");
    }

    @ApiInformation(function = "修改分类名称",menu = "商品分类")
    @AdminOperationLog(value = "修改分类名称",logType = LogType.UPDATE)
    @ApiOperation(value="修改分类名称")
    @RequestMapping(value = "/categories/name",method = RequestMethod.PATCH)
    public CommonResult<Object> setCateName(@RequestBody @Valid CateIdAndNameWrapper wrapper){
        prodCateService.updateCateName(wrapper.getCateId(),wrapper.getName());
        return CommonResult.success(null,"设置分类名称成功");
    }

    @ApiInformation(function = "修改分类显示状态",menu = "商品分类")
    @AdminOperationLog(value = "修改分类状态",logType = LogType.UPDATE)
    @ApiOperation(value="修改分类状态")
    @RequestMapping(value = "/categories/show",method = RequestMethod.PATCH)
    public CommonResult<Object> setCateName(@RequestBody @Valid CateIdAndShowWrapper wrapper){
        prodCateService.updateCateShow(wrapper.getCateId(),wrapper.getShow());
        return CommonResult.success(null,"设置分类显示状态成功");
    }

    @ApiInformation(function = "修改分类备注",menu = "商品分类")
    @AdminOperationLog(value = "修改分类备注",logType = LogType.UPDATE)
    @ApiOperation(value="修改分类备注",notes = "所属菜单:商品分类")
    @RequestMapping(value = "/categories/remarks",method = RequestMethod.PATCH)
    public CommonResult<Object> setCateRemarks(@RequestBody CateIdAndRemarksWrapper wrapper){
        prodCateService.updateCateRemarks(wrapper.getCateId(),wrapper.getRemarks());
        return CommonResult.success(null,"设置分类备注成功");
    }

    @ApiInformation(function = "添加分类",menu = "商品分类")
    @AdminOperationLog(value = "添加分类",logType = LogType.ADD)
    @ApiOperation(value = "添加分类")
    @RequestMapping(value="/categories",method = RequestMethod.POST)
    public CommonResult<Object> addCate(@RequestBody @Valid NewCategoryModel cateModel){
        CategoryDTO cateDTO = new CategoryDTO();
        cateDTO.setName(cateModel.getName());
        cateDTO.setPid(cateModel.getPid());
        cateDTO.setIsShow(cateModel.getIsShow());
        cateDTO.setRemarks(cateModel.getRemarks());
        cateDTO.setIsFloor(cateModel.getIsFloor() );
        prodCateService.saveCate(cateDTO);
        return CommonResult.success(null,"添加分类成功");
    }

    @ApiInformation(function = "删除分类",menu = "商品分类")
    @AdminOperationLog(value = "删除分类",logType = LogType.DELETE)
    @ApiOperation(value = "删除分类",notes = "所属菜单:商品分类")
    @RequestMapping(value="/categories/{cateId}",method = RequestMethod.DELETE)
    public CommonResult<Object> deleteCate(@PathVariable Integer cateId){
        prodCateService.deleteCate(cateId);
        return CommonResult.success(null,"删除分类成功");
    }



}
